package com.seckill.canal.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.annotation.CanalEventListener;
import com.itheima.canal.annotation.DeleteListen;
import com.itheima.canal.annotation.InsertListen;
import com.itheima.canal.annotation.UpdateListen;
import com.seckill.goods.pojo.Sku;
import com.seckill.search.feign.SkuInfoFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CanalEventListener
@Slf4j
public class GoodsSkuListener {

    @Autowired
    private SkuInfoFeign skuInfoFeign;


    /**
     * 修改监听
     */
    @UpdateListen(destination = "example", schema = "seckill_goods", table = "tb_sku")
    public void onEventModify(CanalEntry.RowData rowData) {
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();//更新前
        Map<String, String> mapBefore = beforeColumnsList.stream().collect(Collectors.toMap(CanalEntry.Column::getName, CanalEntry.Column::getValue));
        for (CanalEntry.Column column : beforeColumnsList) {
            //log.info("更新前数据为: -> " + "字段名为: " + column.getName() + ", 值为: " + column.getValue());
        }
        log.info("更新前数据为: -> " + mapBefore);

        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();//更新后的数据
        Map<String, String> mapAfter = afterColumnsList.stream().collect(Collectors.toMap(CanalEntry.Column::getName, CanalEntry.Column::getValue));
        Sku sku = BeanUtil.mapToBean(mapAfter, Sku.class, true, CopyOptions.create());
        for (CanalEntry.Column column : afterColumnsList) {
            //log.info("更新后数据为: -> " + "字段名为: " + column.getName() + ", 值为: " + column.getValue());
        }
        log.info("更新后数据为: -> " + mapAfter);

        //同步到ES
        try {
            skuInfoFeign.addIndex(sku);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("同步到ES失败!, 商品信息为: " + sku + ", 原因为: " + e.getMessage());
        }

    }


    /**
     * 新增监听
     */
    @InsertListen(destination = "example", schema = "seckill_goods", table = "tb_sku")
    public void ListenInsert(CanalEntry.RowData rowData) {
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();//更新前
        Map<String, String> mapAfter = afterColumnsList.stream().collect(Collectors.toMap(CanalEntry.Column::getName, CanalEntry.Column::getValue));
        Sku sku = BeanUtil.mapToBean(mapAfter, Sku.class, true, CopyOptions.create());
        //log.info("更新之前的数据为: {}", beforeColumnsList);
//        for (CanalEntry.Column column : afterColumnsList) {
//            log.info("新增后数据为: -> " + "字段名为: " + column.getName() + ", 值为: " + column.getValue());
//        }
        log.info("新增后数据为: -> " + mapAfter);
        //同步到ES
        try {
            skuInfoFeign.addIndex(sku);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("同步到ES失败!, 商品信息为: " + sku + ", 原因为: " + e.getMessage());
        }

    }

    /**
     * 删除监听
     */
    @DeleteListen(destination = "example", schema = "seckill_goods", table = "tb_sku")
    public void ListenDelete(CanalEntry.RowData rowData) {

        //删除之前的数据
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();//更新后的数据
        Map<String, String> mapBefore = beforeColumnsList.stream().collect(Collectors.toMap(CanalEntry.Column::getName, CanalEntry.Column::getValue));
        Sku sku = BeanUtil.mapToBean(mapBefore, Sku.class, true, CopyOptions.create());
        log.info("删除前数据为: -> " + mapBefore);
        //log.info("更新之后的数据为: {}", afterColumnsList);
//        for (CanalEntry.Column column : beforeColumnsList) {
//            log.info("删除前数据为: -> " + "字段名为: " + column.getName() + ", 值为: " + column.getValue());
//        }
        //删除后,就为空了
        skuInfoFeign.deleteById(sku.getId());
    }
}
